package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.GroupUserConvert;
import cn.com.blueInfo.bpm.person.entity.GroupUser;
import cn.com.blueInfo.bpm.person.repository.GroupUserRepository;
import cn.com.blueInfo.bpm.person.request.GroupUserDTO;
import cn.com.blueInfo.bpm.person.response.GroupUserVO;
import cn.com.blueInfo.bpm.person.service.GroupUserService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 群组固定人员表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class GroupUserServiceImpl implements GroupUserService {

    @Autowired
    private GroupUserRepository groupUserRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private GroupUserConvert groupUserConvert;

    /**
     * 新增群组固定人员
     * @param groupUserDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<Integer> addGroupUser(GroupUserDTO groupUserDTO) {
        GroupUser groupUser = groupUserConvert.dtoToEntity(groupUserDTO);
        groupUser.setId(snowflakeIdGenerator.nextId());
        Integer addSize = groupUserRepository.createGroupUser(groupUser);
        return ResultInfo.<Integer>builder().data(addSize).message("群组固定人员成功新增 {} 条").build();
    }

    /**
     * 批量新增群组固定人员
     * @param groupUserDTOList 群组固定人员参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddGroupUser(List<GroupUserDTO> groupUserDTOList) {
        List<GroupUser> groupUserList = groupUserConvert.dtoToEntity(groupUserDTOList);
        for (GroupUser groupUser : groupUserList) groupUser.setId(snowflakeIdGenerator.nextId());
        Integer addSize = groupUserRepository.batchCreateGroupUser(groupUserList);
        return ResultInfo.<Integer>builder().data(addSize).message("群组固定人员成功新增 {} 条").build();
    }

    /**
     * 删除群组固定人员
     * @param groupUserId 群组固定人员ID
     */
    @Override
    public ResultInfo<Integer> removeGroupUser(Long groupUserId) {
        Integer removeSize = groupUserRepository.deleteGroupUser(groupUserId);
        return ResultInfo.<Integer>builder().data(removeSize).message("群组固定人员成功删除 {} 条").build();
    }

    /**
     * 批量删除群组固定人员
     * @param groupUserIdList 群组固定人员ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveGroupUser(List<Long> groupUserIdList) {
        Integer removeSize = groupUserRepository.batchDeleteGroupUser(groupUserIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("群组固定人员成功删除 {} 条").build();
    }

    /**
     * 更新部分群组固定人员
     * @param groupUserUpdatePartialDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<Integer> modifyGroupUserPartial(GroupUserDTO.UpdatePartialDTO groupUserUpdatePartialDTO) {
        GroupUser groupUser = groupUserConvert.dtoToEntity(groupUserUpdatePartialDTO);
        Integer modifySize = groupUserRepository.updateGroupUserPartial(groupUser);
        return ResultInfo.<Integer>builder().data(modifySize).message("群组固定人员成功更新 {} 条").build();
    }

    /**
     * 更新全部群组固定人员
     * @param groupUserDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<Integer> modifyGroupUser(GroupUserDTO groupUserDTO) {
        GroupUser groupUser = groupUserConvert.dtoToEntity(groupUserDTO);
        Integer modifySize = groupUserRepository.updateGroupUser(groupUser);
        return ResultInfo.<Integer>builder().data(modifySize).message("群组固定人员成功更新 {} 条").build();
    }

    /**
     * 查询一条群组固定人员
     * @param groupUserId 群组固定人员ID
     */
    @Override
    public ResultInfo<GroupUserVO> queryGroupUserById(Long groupUserId) {
        GroupUser groupUser = groupUserRepository.getGroupUserById(groupUserId);
        GroupUserVO groupUserVO = groupUserConvert.entityToVO(groupUser);
        return ResultInfo.<GroupUserVO>builder().data(groupUserVO).message("群组固定人员查询成功").build();
    }

    /**
     * 分页查询群组固定人员
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<Page<GroupUserVO>> queryGroupUserPage(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        Page<GroupUser> groupUserPage = groupUserRepository.getGroupUserPage(groupUserQueryDTO);
        Page<GroupUserVO> resultData = groupUserConvert.convertPage(groupUserPage);
        return ResultInfo.<Page<GroupUserVO>>builder()
                .data(resultData)
                .message("群组固定人员分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 群组固定人员 数量
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<Long> queryGroupUserCount(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        Long resultCount = groupUserRepository.getGroupUserCount(groupUserQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("群组固定人员数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 群组固定人员
     * @param groupUserQueryDTO 群组固定人员参数
     */
    @Override
    public ResultInfo<List<GroupUserVO>> queryGroupUserList(GroupUserDTO.QueryDTO groupUserQueryDTO) {
        List<GroupUser> groupUserList = groupUserRepository.getGroupUserList(groupUserQueryDTO);
        List<GroupUserVO> resultData = groupUserConvert.entityListToVoList(groupUserList);
        return ResultInfo.<List<GroupUserVO>>builder()
                .data(resultData)
                .message("群组固定人员分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
